<template>
  <div class="container">
    <div class="header">
      <!--    输入框-->
      <el-form :inline="true" :model="searchForm" ref="searchRef">
        <el-form-item label="内容检索" prop="content">
          <el-input
            style="width: 200px"
            placeholder="请输入内容"
            size="mini"
            v-model="searchForm.content"
          ></el-input>
        </el-form-item>
        <el-form-item label="操作用户" prop="username">
          <el-input
            style="width: 120px"
            placeholder="请输入用户"
            size="mini"
            v-model="searchForm.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="time">
          <el-date-picker
            style="margin-right: 10px"
            v-model="searchForm.startTime"
            type="datetime"
            placeholder="开始日期"
            value-format="YYYY-MM-DD hh:mm:ss"
            size="small"
          >
          </el-date-picker>
          <el-date-picker
            v-model="searchForm.endTime"
            type="datetime"
            placeholder="结束日期"
            value-format="YYYY-MM-DD hh:mm:ss"
            size="small"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <TsButton
            perms="logs:get"
            label="搜索"
            type="primary"
            size="mini"
            icon="el-icon-search"
            @click="search"
          ></TsButton>
          <el-button size="mini" icon="el-icon-refresh-right" @click="reset"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <!--    表格区域-->
    <div class="table" style="height: calc(100% - 160px); overflow-y: auto">
      <el-table
        :header-cell-style="{ background: '#FEF6F6' }"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column prop="username" label="用户"> </el-table-column>
        <el-table-column prop="createTime" label="时间"> </el-table-column>
        <el-table-column prop="id" label="工号"> </el-table-column>
        <el-table-column prop="operation" show-overflow-tooltip label="内容">
        </el-table-column>
        <el-table-column prop="ip" label="IP"> </el-table-column>
      </el-table>
    </div>
    <el-pagination
      style="float: right; margin: 20px"
      @size-change="pageSizeChange"
      @current-change="pageCurrentChange"
      :current-page="pageNum"
      :page-size="pageSize"
      layout=" prev, pager, next"
      :total="totalNum"
      background
    >
    </el-pagination>
  </div>
</template>

<script>
import { onMounted, ref } from "vue";
import { queryLog } from "../../api/system/operation-logApi";
import commonPageRepository from "../../composables/common-page-repository";
import TsButton from "../../components/TsButton";

export default {
  name: "OperationLog",
  components: { TsButton },
  setup() {
    //表格数据
    const tableData = ref([]);
    //分页查询
    const pageNum = ref(1);
    const pageSize = ref(50);
    const totalNum = ref(Number);
    //搜索表单数据
    const searchForm = ref({});
    //搜索表单标识
    const searchRef = ref(null);
    //搜索功能
    function search() {
      queryLog({
        pageSize: pageSize.value,
        pageNum: pageNum.value,
        content: searchForm.value.content,
        startTime: searchForm.value.startTime,
        endTime: searchForm.value.endTime,
        username: searchForm.value.username,
      }).then((res) => {
        tableData.value = res.data.content;
        totalNum.value = res.data.totalElements;
      });
    }
    //重置功能
    function reset() {
      searchForm.value.startTime = "";
      searchForm.value.endTime = "";
      searchRef.value.resetFields();
      getTableData();
    }
    //获取分页数据
    function getTableData() {
      queryLog({
        pageNumber: pageNum.value,
        pageSize: pageSize.value,
      }).then((res) => {
        tableData.value = res.data.content;
        totalNum.value = res.data.totalElements;
      });
    }
    const { pageSizeChange, pageCurrentChange } = commonPageRepository(
      pageSize,
      pageNum,
      getTableData
    );
    onMounted(() => {
      getTableData();
    });
    return {
      tableData,
      pageSize,
      pageNum,
      totalNum,
      pageSizeChange,
      pageCurrentChange,
      searchForm,
      searchRef,
      search,
      reset,
    };
  },
};
</script>

<style scoped lang="scss">
.container {
  position: relative;
  height: calc(100vh - 180px);
  overflow-y: auto;
  background: #ffffff;
  border-radius: 6px;
  opacity: 1;
  box-shadow: 0px 0px 16px rgba(96, 96, 96, 0.16);
  .header {
    padding: 10px 20px;
    font-size: 14px;
    display: flex;
    align-items: center;
    .hd-input {
      margin-right: 10px;
      display: flex;
      align-items: center;
    }
  }
  .table {
    padding: 0 20px;
  }
  .pagination {
    position: absolute;
    bottom: 20px;
    right: 20px;
  }
}
</style>
